package com.flexrrb.controls.groupTextInputClasses
{
	import com.flexrrb.events.GroupTextInputEvent;
	import com.flexrrb.skin.DefaultGroupTextInputItemSkin;
	
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.core.UIComponent;
	import mx.skins.ProgrammaticSkin;

	public class GroupTextInputItem extends UIComponent
	{
		public static const STATE_SELECTED:String = "stateSelected";
		public static const STATE_NORMAL:String = "stateNormal";
		
		private var _text:String;
		private var _invalidateTextFlag:Boolean;
		
		protected var _label:Label;
		protected var _removeIcon:Image;
		
		private var _state:String = STATE_NORMAL;
		private var _invalidateStateFlag:Boolean;
		
		[Embed("../../../../assets/cancel.png")]
		public static var cancelImg:Class;
		
		private var _skin:ProgrammaticSkin;
		
		public function GroupTextInputItem()
		{
			super();
			_skin = new DefaultGroupTextInputItemSkin() as ProgrammaticSkin;
			this.addEventListener(FocusEvent.FOCUS_IN, onFocusIn, false, 0, true);
			this.addEventListener(MouseEvent.CLICK, onMouseClick, false, 0, true);
			this.addEventListener(FocusEvent.KEY_FOCUS_CHANGE, onKeyFocusChanged, false, 0, true);
			this.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut, false, 0, true);
		}
		
		private function onKeyFocusChanged(event:FocusEvent):void
		{
			if(focusManager.getFocus() == this)
			{
				trace("focus in : ", this.text);
			}
			else
			{
				focusManager.setFocus(focusManager.getNextFocusManagerComponent());
			}
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			this.setFocus();
		}
		
		private function onFocusOut(event:FocusEvent):void
		{
			trace("losing focus...");
			this.state = STATE_NORMAL;
			
		}
		
		private function onFocusIn(event:FocusEvent):void
		{
			this.state = STATE_SELECTED;
		}
		
		public function get state():String
		{
			return _state;
		}
		
		public function set state(value:String):void
		{
			_state = value;
			
			if(_state == STATE_SELECTED)
			{
				this.addEventListener(KeyboardEvent.KEY_DOWN, onItemKeyUp, false, 0, true);
				(_skin as DefaultGroupTextInputItemSkin).state = STATE_SELECTED;
				_invalidateStateFlag = true;
			}
			else
			{
				(_skin as DefaultGroupTextInputItemSkin).state = STATE_NORMAL;
				this.removeEventListener(KeyboardEvent.KEY_DOWN, onItemKeyUp);
			}
			
			invalidateDisplayList();
		}
		
		private function onItemKeyUp(event:KeyboardEvent):void
		{
			trace("item key up...");
			event.stopImmediatePropagation();
			if(event.keyCode == Keyboard.BACKSPACE || event.keyCode == Keyboard.DELETE)
			{
				dispatchEvent(new GroupTextInputEvent(GroupTextInputEvent.REMOVE_ITEM));	
			}
		}
		
		public function set text(value:String):void
		{
			_text = value;
			_invalidateTextFlag = true;
			invalidateProperties();
			invalidateSize();
		}
		
		public function get text():String
		{
			return _text;	
		}
		1
		override protected function createChildren():void
		{
			super.createChildren();
			
			_label = new Label();
			_label.truncateToFit = true;
			
			_removeIcon = new Image();
			_removeIcon.source = cancelImg;
			
			_removeIcon.addEventListener(MouseEvent.CLICK, onRemoveClick, false, 0, true);
			
			addChild(_skin);
			addChild(_label);
		    addChild(_removeIcon);
		}
		
		private function onRemoveClick(event:MouseEvent):void
		{
			dispatchEvent(new GroupTextInputEvent(GroupTextInputEvent.REMOVE_ITEM));
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			
			if(_invalidateTextFlag)
			{
				_label.text = _text;
			}
		}
		
		//measure will be called when there's no size specified
		override protected function measure():void
		{
			super.measure();
			
			if(_invalidateTextFlag)
			{
				var totalWidth:Number = _label.getExplicitOrMeasuredWidth() + 2 + _removeIcon.getExplicitOrMeasuredWidth() + 2;
				trace("maxWidth: ", maxWidth, " totalWidth: ", totalWidth);
				if(totalWidth < maxWidth)
				{
					this.measuredWidth = _label.getExplicitOrMeasuredWidth() + 2 + _removeIcon.getExplicitOrMeasuredWidth() + 2;
				}
				else
				{
					trace("readjusting width of label...");
					_label.measuredWidth = this.maxWidth - _removeIcon.getExplicitOrMeasuredWidth() - 4;
					this.measuredWidth = this.maxWidth;
				}
				this.measuredHeight = _label.getExplicitOrMeasuredHeight();
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			_skin.setActualSize(unscaledWidth, unscaledHeight);
			_removeIcon.setActualSize(_removeIcon.getExplicitOrMeasuredWidth(), _removeIcon.getExplicitOrMeasuredHeight());
			_label.setActualSize(_label.getExplicitOrMeasuredWidth(), _label.getExplicitOrMeasuredHeight());
			
			if(_invalidateTextFlag)
			{
				_label.move(2, this.getExplicitOrMeasuredHeight() * 0.5 - _label.getExplicitOrMeasuredHeight() * 0.5);
				_removeIcon.move(_label.getExplicitOrMeasuredWidth() + 3, this.getExplicitOrMeasuredHeight() * 0.5 - _removeIcon.getExplicitOrMeasuredHeight() * 0.5);
			}
			
			//_skin.setActualSize(_removeIcon.x + _removeIcon.getExplicitOrMeasuredWidth() + 4, unscaledHeight);
		}
	}
}